package com.hckj.managersystemdemo.repository;

import com.hckj.managersystemdemo.entity.Users;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Author
 * @Description TODO
 */
@Repository
public class UserRepositoryImp implements UsersRepository {
    //主键id
    private static AtomicInteger id = new AtomicInteger(5);
    static List<Users> users = new ArrayList<>();
    static{
        Collections.addAll(users,new Users(1,"zhangsan","123456","张三","男"),
                new Users(2,"lisi","123456","李四","女"),
                new Users(3,"wangwu","123456","王五","男"),
                new Users(4,"zhaoliu","123456","赵柳","女"),
                new Users(5,"wangba","123456","网吧","男"));
    }
    @Override
    public List<Users> findAll() {
        return users;
    }

    @Override
    public int deleteUserById(int id) {
        int num = 0;
        Iterator<Users> iterator = users.iterator();
        while(iterator.hasNext()){
            Users users = iterator.next();
            if (users.getId()==id){
                iterator.remove();
                num = 1;
            }
        }
        return num;
    }

    @Override
    public boolean addUser(Users user) {
        user.setId(id.addAndGet(1));
        return Collections.addAll(users, user);
    }
}
